import React from 'react'
import {Icon, Badge, Dropdown, Menu, Modal, Button, Tooltip} from 'antd'
import {connect} from 'react-redux'
//import { inject, observer } from 'mobx-react'
import { Link, withRouter } from 'react-router-dom'
// import { isAuthenticated } from '../../utils/Session'
import {
    UserOutlined,SmileTwoTone
} from '@ant-design/icons';
import {InsertRowRightOutlined} from "@ant-design/icons";
import {login} from "../api/Security";
import {setToken} from "../utils/auth";
import {logout as server_logout} from "../api/Security";
//withRouter一定要写在前面，不然路由变化不会反映到props中去
//@withRouter
class HeaderBar extends React.Component {
    state = {
        icon: 'arrows-alt',
        count: 100,
        visible: false,
        //avatar: require('./img/04.jpg')
        avatar:null
    }

    componentDidMount () {

    }

    componentWillUnmount () {

    }


    logout = () => {
       // this.props.appStore.toggleLogin(false)
        //去掉store当中的值 cookie  当中的值 sessionid?
        //console.log("this.props.location:"+this.props.location);
        //console.log("this.props.location.pathname:"+this.props.location.pathname)
        //this.props.history.push(this.props.location.pathname)

        let r=this.props.logout_prop();

        this.props.history.push("/login")
    }

    render () {
        const {icon, count, visible, avatar} = this.state
        //const {appStore, collapsed, location} = this.props
        const {collapsed, location} = this.props
        // const notLogin = (
        //     <div>
        //         <Link to={{pathname: '/login', state: {from: location}}} style={{color: 'rgba(0, 0, 0, 0.65)'}}>登录</Link>&nbsp;
        //         <img src={require('../../assets/img/defaultUser.jpg')} alt=""/>
        //     </div>
        // )
        const menu = (
            <Menu className='menu'>
                <Menu.ItemGroup title='用户中心' className='menu-group'>
                    <Menu.Item>你好 -
                        {/*{isAuthenticated()}*/}
                        </Menu.Item>
                    <Menu.Item>个人信息</Menu.Item>
                    <Menu.Item>
                        <span
                            onClick={this.logout}
                        >退出登录</span></Menu.Item>
                </Menu.ItemGroup>
                <Menu.ItemGroup title='设置中心' className='menu-group'>
                    <Menu.Item>个人设置</Menu.Item>
                    <Menu.Item>系统设置</Menu.Item>
                </Menu.ItemGroup>
            </Menu>
        )
        const login = (
            <Dropdown overlay={menu}>
                <UserOutlined />
            </Dropdown>
        )
        return (
            <span>
                {login}
            </span>
        )
    }
}
// //将store.dispatch方法挂载到props上
const mapDispatchToProps = (dispatch) => {
    return {
        logout_prop() {
            let r= server_logout().then(
                (res) => {
                     if (res.code === "200") {
                            const action = {
                                type: 'login_token',
                                login_token: null
                            }
                            //存放到cookie
                            setToken('')
                            dispatch(action)
                            //继续跳转......
                            //this.history.push('/main')
                            return 1;


                    }else{   //登录失败 用户名 密码错误 主要走这个

                        return -1;
                    }
                },
                (error) => {
                    console.log("get response failed!");
                    return -3;
                }
            );
            return r;
        }
    }
}
export default withRouter(connect(null, mapDispatchToProps)(HeaderBar))

